package bomberman.model;

import java.awt.Graphics;
import java.awt.Rectangle;

import bomberman.MainGame;
import bomberman.runable.MyRunableMoveEnemy;
import bomberman.runable.MyRunableRemoveEnemy;
import bomberman.swing.BomJPanel;
import bomberman.util.CommonTouch;
import bomberman.util.Constants;
import bomberman.util.GameMap;

/**
 * 敌人类
 * @author EX-FUHUICHAO001
 *	2016-9-26 09:31:19
 */
public class Enemy {
	private int x;
	private int y;
	
	// 敌人样式
	private int style = Constants.ENEMY_STYLE_NORMAL;

	// 定义本次移动方向
	private char direction;
	
	// 定义敌人是否存在
	private boolean isLive = true;
	
	// 定义本次本方向移动距离
	private int length;
	
	public Enemy(int x, int y){
		this.x = x;
		this.y = y;
//		MyRunableThreadPool.getExecutorService().execute(new MyRunableMoveEnemy(this));
		new Thread(new MyRunableMoveEnemy(this)).start();
	}
	
	public Rectangle getRect(){
		return new Rectangle(x, y, 30, 30);
	}
	
	public void draw(Graphics g) {
		g.drawImage(GameMap.img_enemy_array[style], x, y, MainGame.jPanelIndex);
	}
	
	
	/**
	 * 敌人随机移动
	 */
	public void move() throws InterruptedException{
		// 随机方向
		direction = Constants.MOVE_DIRECTION[BomJPanel.random.nextInt(Constants.MOVE_DIRECTION.length)];
		// 随机距离
		length = BomJPanel.random.nextInt(Constants.LENGTH_ENEMY)*30;
		
		// 使用临时变量，在敌人移动前判断是否有效移动
		int tempx = x, tempy = y;
		
		switch (direction) {
			case Constants.MOVE_DOWN:
				for(int i = 0 ; i < length ; i ++){
					if(! isLive) break; // 若敌人死亡，则立刻停止移动
					tempy += Constants.SPEED_ENEMY;
					if(checkEnemyTouch(tempx, tempy)){
						break;
					}else{
						y = tempy;
					}
					Thread.sleep(Constants.ENEMY_MOVE_SLEEP_TIME);
				}
				break;
			case Constants.MOVE_UP:
				for(int i = 0 ; i < length ; i ++){
					if(! isLive) break;
					tempy -= Constants.SPEED_ENEMY;
					if(checkEnemyTouch(tempx, tempy)){
						break;
					}else{
						y = tempy;
					}
					Thread.sleep(Constants.ENEMY_MOVE_SLEEP_TIME);
				}
				break;
			case Constants.MOVE_LEFT:
				for(int i = 0 ; i < length ; i ++){
					if(! isLive) break;
					tempx -= Constants.SPEED_ENEMY;
					if(checkEnemyTouch(tempx, tempy)){
						break;
					}else{
						x = tempx;
					}
					Thread.sleep(Constants.ENEMY_MOVE_SLEEP_TIME);
				}
				break;
			case Constants.MOVE_RIGHT:
				for(int i = 0 ; i < length ; i ++){
					if(! isLive) break;
					tempx += Constants.SPEED_ENEMY;
					if(checkEnemyTouch(tempx, tempy)){
						break;
					}else{
						x = tempx;
					}
					Thread.sleep(Constants.ENEMY_MOVE_SLEEP_TIME);
				}
				break;
			default:
				break;
		}
	}
	
	public boolean checkEnemyTouch(int x, int y){
		return CommonTouch.checkIfTouchBorder(x, y)
				|| CommonTouch.checkIfTouchStone(x, y) 
				|| CommonTouch.checkIfTouchWall(x, y) 
				|| CommonTouch.checkIfEnemyTouchBom(x, y);
	}
	
	/**
	 * 敌人移除
	 */
	public void remove(){
		isLive = false;
		style = Constants.ENEMY_STYLE_DIE;
		new Thread(new MyRunableRemoveEnemy(this)).start();
	}
	
	
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public char getDirection() {
		return direction;
	}

	public void setDirection(char direction) {
		this.direction = direction;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public boolean isLive() {
		return isLive;
	}

	public void setLive(boolean isLive) {
		this.isLive = isLive;
	}

	public int getStyle() {
		return style;
	}

	public void setStyle(int style) {
		this.style = style;
	}

}
